﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoNet.Common.Utilities;
using Shop.Intermediate;
using Shop.Intermediate.Data;
using Shop.Core;

namespace Shop.WebApp.UserControl
{
    public partial class SplitPage<T> : System.Web.UI.UserControl
    {
        private static Dictionary<Uri, IPageSplit> m_pageSplitDic;
        private Uri m_url;
        private int m_pageCount;
        private int m_recordCount;

        private LinkButton lbHomePage;
        private LinkButton lbPreviousPage;
        private LinkButton lbNextPage;
        private LinkButton lbEndPage;
        private DropDownList ddlCurrentPage;
        private Label lblPageSize;
        private Label lblPageCount;
        private Label lblRecordCount;

        public SplitPage()
        {
            if (m_pageSplitDic == null)
            {
                m_pageSplitDic = new Dictionary<Uri, IPageSplit>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            lbHomePage = new LinkButton();
            lbHomePage.CausesValidation = false;
            lbHomePage.Text = " 首页 ";
            lbHomePage.Click += new EventHandler(lbHomePage_Click);
            lbHomePage.ID = "lbHomePage";

            lbPreviousPage = new LinkButton();
            lbPreviousPage.CausesValidation = false;
            lbPreviousPage.Text = " 上一页 ";
            lbPreviousPage.Click += new EventHandler(lbPreviousPage_Click);
            lbPreviousPage.ID = "lbPreviousPage";

            lbNextPage = new LinkButton();
            lbNextPage.CausesValidation = false;
            lbNextPage.Text = " 下一页 ";
            lbNextPage.Click += new EventHandler(lbNextPage_Click);
            lbNextPage.ID = "lbNextPage";

            lbEndPage = new LinkButton();
            lbEndPage.CausesValidation = false;
            lbEndPage.Text = " 末页 ";
            lbEndPage.Click += new EventHandler(lbEndPage_Click);
            lbEndPage.ID = "lbEndPage";

            ddlCurrentPage = new DropDownList();
            ddlCurrentPage.ID = "ddlCurrentPage";
            for (int i = 1; i <= this.PageCount; i++)
            {
                ddlCurrentPage.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            ddlCurrentPage.SelectedIndexChanged += new EventHandler(ddlCurrentPage_SelectedIndexChanged);
            ddlCurrentPage.AutoPostBack = true;
            ddlCurrentPage.CausesValidation = false;

            lblPageSize = new Label();
            lblPageSize.ID = "lblPageSize";
            lblPageSize.Text = " 每页" + this.PageSize.ToString() + "条 ";
            this.Controls.Add(lblPageSize);
            lblPageSize.Visible = false;

            lblPageCount = new Label();
            lblPageCount.ID = "lblPageCount";
            lblPageCount.Text = " 共" + this.PageCount.ToString() + "页 ";

            lblRecordCount = new Label();
            lblRecordCount.ID = "lblRecordCount";
            lblRecordCount.Text = " 共" + this.RecordCount.ToString() + "条 ";

            this.Controls.Add(lbHomePage);
            this.Controls.Add(lbPreviousPage);
            this.Controls.Add(lbNextPage);
            this.Controls.Add(lbEndPage);
            Label lblFirst = new Label();
            lblFirst.Text = "第";
            this.Controls.Add(lblFirst);
            this.Controls.Add(ddlCurrentPage);
            Label lblEnd = new Label();
            lblEnd.Text = "页";
            this.Controls.Add(lblEnd);

            this.Controls.Add(lblPageCount);
            this.Controls.Add(lblRecordCount);

        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbHomePage_Click(object sender, EventArgs e)
        {
            if (this.CurrentPageNo == 1)
            {
                MessageBox.Show(this.Page, "已经是第一页");
                return;
            }
            this.CurrentPageNo = 1;
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            BindData();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbPreviousPage_Click(object sender, EventArgs e)
        {
            if (this.CurrentPageNo == 1)
            {
                MessageBox.Show(this.Page, "已经是第一页");
                return;
            }
            this.CurrentPageNo = this.CurrentPageNo - 1;
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            BindData();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbNextPage_Click(object sender, EventArgs e)
        {
            if (this.CurrentPageNo == this.PageCount)
            {
                MessageBox.Show(this.Page, "已经是最后一页！");
                return;
            }
            this.CurrentPageNo = this.CurrentPageNo + 1;
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            BindData();
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.CurrentPageNo.ToString() == ddlCurrentPage.SelectedValue)
            {
                MessageBox.Show(this.Page, "已经是第" + ddlCurrentPage.SelectedValue.ToString() + "页");
                return;
            }
            this.CurrentPageNo = StringHelper.ObjectToInt(ddlCurrentPage.SelectedValue);
            BindData();
        }


        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbEndPage_Click(object sender, EventArgs e)
        {
            if (this.CurrentPageNo == this.PageCount)
            {
                MessageBox.Show(this.Page, "已经是最后一页！");
                return;
            }
            this.CurrentPageNo = this.PageCount;
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void BindData()
        {
            m_pageCount = this.PageCount;
            m_recordCount = this.RecordCount;

            if (string.IsNullOrEmpty(this.Container) || this.PageSplit == null)
                return;

            Control control = this.Page.FindControl(this.Container);
            if (control == null)
                return;

            IEnumerable<T> query = this.PageSplit.PageResultQuery<T>(this.Condition
                 , this.ConditionValue, this.Orderby, this.OrderbyValue, this.CurrentPageNo, this.PageSize, out m_pageCount
                 , out m_recordCount);

            string controlType = control.GetType().Name;
            switch (controlType)
            {
                case "GridView":
                    (control as GridView).DataSource = query;
                    (control as GridView).DataBind();
                    break;
                case "DataList":
                    (control as DataList).DataSource = query;
                    (control as DataList).DataBind();
                    break;
                case "Repeater":
                    (control as Repeater).DataSource = query;
                    (control as Repeater).DataBind();
                    break;
                default:
                    return;
            }

            this.RecordCount = m_recordCount;
            this.PageCount = m_pageCount;
        }

        public void RefreshPageNo()
        {
            ddlCurrentPage.SelectedValue = this.CurrentPageNo.ToString();
            lblPageSize.Text = " 每页" + this.PageSize.ToString() + "条 ";
            lblPageCount.Text = " 共" + this.PageCount.ToString() + "页 ";
            lblRecordCount.Text = " 共" + this.RecordCount.ToString() + "条 ";
        }

        public int PageCount
        {
            get
            {
                return StringHelper.ObjectToInt(ViewState["PageCount"]);
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }

        public int PageSize
        {
            get
            {
                return StringHelper.ObjectToInt(ViewState["PageSize"]);
            }
            set
            {
                ViewState["PageSize"] = value;
            }
        }

        public int RecordCount
        {
            get
            {
                return StringHelper.ObjectToInt(ViewState["RecordCount"]);
            }
            set
            {
                ViewState["RecordCount"] = value;
            }
        }

        public int CurrentPageNo
        {
            get
            {
                return StringHelper.ObjectToInt(ViewState["CurrentPageNo"]);
            }
            set
            {
                ViewState["CurrentPageNo"] = value;
            }
        }

        public string Condition
        {
            get
            {
                if (ViewState["Condition"] == null)
                    return null;
                else
                    return ViewState["Condition"].ToString();
            }
            set
            {
                ViewState["Condition"] = value;
            }
        }

        public object[] ConditionValue
        {
            get
            {
                if (ViewState["ConditionValue"] == null)
                    return null;
                else
                    return ViewState["ConditionValue"] as object[];
            }
            set
            {
                ViewState["ConditionValue"] = value;
            }
        }

        public string Orderby
        {
            get
            {
                if (ViewState["Orderby"] == null)
                    return null;
                else
                    return ViewState["Orderby"].ToString();
            }
            set
            {
                ViewState["Orderby"] = value;
            }
        }

        public object[] OrderbyValue
        {
            get
            {
                if (ViewState["OrderbyValue"] == null)
                    return null;
                else
                    return ViewState["OrderbyValue"] as object[];
            }
            set
            {
                ViewState["OrderbyValue"] = value;
            }
        }


        public string Container
        {
            get
            {
                if (ViewState["Container"] == null)
                    return string.Empty;
                else
                    return ViewState["Container"].ToString();
            }
            set
            {
                ViewState["Container"] = value;
            }
        }

        public IPageSplit PageSplit
        {
            #region
            //get
            //{
            //    if (ViewState["PageSplit"] == null)
            //        return null;
            //    else
            //    {
            //        object o = SerializationHelper.DeserializeBinary(ViewState["PageSplit"] as byte[]);

            //        return o as IPageSplit;
            //    }
            //}
            //set
            //{
            //    byte[] b = SerializationHelper.SerializeBinary(value);
            //    ViewState["PageSplit"] = b;
            //}
            #endregion
            get
            {
                m_url = Request.Url;
                return m_pageSplitDic[m_url];
            }
            set
            {
                m_url = Request.Url;
                if (m_pageSplitDic.ContainsKey(m_url))
                {
                    m_pageSplitDic.Remove(m_url);
                }
                m_pageSplitDic.Add(m_url, value);
            }
        }
    }
}